package com.mineapp.base

import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.Toolbar
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.tool.Lifecycle
import com.common.R
import com.mineapp.weight.LoadingDialog
import com.mineapp.weight.ToolbarView

/**
 * Created by mac on 2017/8/1.
 */
open abstract class BaseFragment : Fragment() {
    var mToolbar: Toolbar? = null
    var mToolbarLayout: ToolbarView? = null
    var mLifecycle: Lifecycle? = null
    val mLoadingDialog: LoadingDialog by lazy {
        LoadingDialog(activity)
    }

    fun push(cls: Class<out Fragment>?) {
        (activity as? BaseActivity)?.push(cls, null)
    }

    fun push(cls: Class<out Fragment>?, bundle: Bundle?) {
        (activity as? BaseActivity)?.push(cls, bundle)
    }

    override fun onResume() {
        super.onResume()
        mLifecycle?.onResume()
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val contentView = LinearLayout(activity)
        val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT)
        contentView.layoutParams = params
        contentView.orientation = LinearLayout.VERTICAL
        hideActivityTitle()
        if(showToolbar()) {
            initToolbar(contentView)
        }
        val view = bindView(inflater, container, savedInstanceState)
        if(view != null) {
            contentView.addView(view)
        }
        return contentView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mLifecycle?.onCreate(savedInstanceState)
    }

    open fun showToolbar(): Boolean {
        return false
    }

    abstract fun bindView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View?

    fun initToolbar(contentView: LinearLayout?) {
        mToolbarLayout = ToolbarView(activity)
        mToolbar = mToolbarLayout?.mToolbar
        if(mToolbar != null && contentView?.indexOfChild(mToolbar)!! < 0) {
            (mToolbar?.parent as? ViewGroup)?.removeView(mToolbar)
            contentView?.addView(mToolbar, 0)
            mToolbar?.setBackgroundColor(Color.parseColor("#3399ff"))
            mToolbar?.setNavigationIcon(R.mipmap.ic_back_white)
            mToolbar?.setNavigationOnClickListener {
                (activity as? BaseActivity)?.pop()
            }
        }
    }

    fun setTitle(title: String) {
        mToolbarLayout?.setTitle(title)
    }

    fun setMenu(res: Int) {
        mToolbar?.inflateMenu(res)
        mToolbar?.setOnMenuItemClickListener { item: MenuItem? ->
            onMenuItemClick(item)
            return@setOnMenuItemClickListener true
        }
    }

    open fun onMenuItemClick(item: MenuItem?) {

    }

    fun hideActivityTitle() {
        if(showToolbar()) {
            (activity as? BaseActivity)?.mToolbar?.visibility = View.GONE
        } else {
            (activity as? BaseActivity)?.mToolbar?.visibility = View.VISIBLE
        }
    }

    fun setLifecycle(lifecycle: Lifecycle?) {
        mLifecycle = lifecycle
    }

    override fun onDestroyView() {
        mLoadingDialog.dismiss()
        super.onDestroyView()
        mLifecycle?.onDestroy()
    }

    fun showLoading() {
        if (!mLoadingDialog.isShowing && !activity.isFinishing) {
            mLoadingDialog?.show()
        }
    }

    fun hideLoading() {
        mLoadingDialog?.dismiss()
    }
}